<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['BatchTransfer'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="60%"
        style="height: 80%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                转拨卡密
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem :label="t('转拨代理')" type="remoteSelect" v-model="baTable.form.items!.admin_id"
                              prop="admin_id"
                              :input-attr="{ pk: 'ba_admin.id', field: 'nickname', remoteUrl: '/admin/auth.Admin/index?is_daili=1' }"
                              :placeholder="t('Please select field', { field: t('vip.code.admin_id') })"/>



                    <el-row :gutter="10">
                        <el-col :span="12">
                            <FormItem
                                :label="t('起始卡号')"
                                type="number"
                                v-model="baTable.form.items!.start"
                                prop="longitude"
                                :placeholder="t('起始卡号')"
                            />
                        </el-col>
                        <el-col :span="12">
                            <FormItem
                                :label="t('结束卡号')"
                                type="number"
                                v-model="baTable.form.items!.end"
                                prop="latitude"
                                :placeholder="t('结束卡号')"
                            />
                        </el-col>
                    </el-row>
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                           type="primary">
                    {{
                        baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save')
                    }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {FormInstance, FormItemRule} from 'element-plus'
import {inject, reactive, ref} from 'vue'
import {useI18n} from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import {useConfig} from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import {buildValidatorData} from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const {t} = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    number: [buildValidatorData({name: 'number', title: t('vip.code.number')})],
    create_time: [buildValidatorData({name: 'date', title: t('vip.code.create_time')})],
    update_time: [buildValidatorData({name: 'date', title: t('vip.code.update_time')})],
})
</script>

<style scoped lang="scss"></style>
